<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        function ListNode(val, next) {
            this.val = (val===undefined ? 0 : val)
            this.next = (next===undefined ? null : next)
        }
        const h9 = new ListNode(9, null)
        const h8 = new ListNode(8, h9)
        const h7 = new ListNode(7, h8)
        const h6 = new ListNode(6, h7)
        const h5 = new ListNode(5, h6)
        const h4 = new ListNode(4, h5)
        const h3 = new ListNode(3, h4)
        const h2 = new ListNode(2, h3)
        const h1 = new ListNode(1, h2)
        // console.log(h1, h2, h3 ,h4, h5)
        function reverseList(head, n) {
            
        }

        function getEnd(head, n) {
            let cur = head
            let pre = null
            let step = 0
            while(cur && n > step) {
                let next = cur.next
                cur.next = pre
                pre = cur
                cur = next
                step++
            }
            
            if(cur){                
                const hs = getEnd(cur, n)
                head.next = hs
            }            
            return pre
            
        }
        // [1,2,3, 4,5,6, 7,8,9]
        // [3,2,1, 6,5,4, 9,8,7]

        // function reverseAtGj(head, n) {
        //     if(!head) return null
        //     let index = 0
        //     let cur = head
        //     let pre = null
        //     while(index<n && cur) {
        //         let next = cur.next
        //         cur.next = pre
        //         pre = cur
        //         cur = next
        //         index++
        //     }
        //     head.next = reverseAtGj(cur, n)
        //     return pre
        // }
        
        /**
         * @desc 栈的思路实现 
         * */ 
        function reverseAtGj(head, n) {
            if(!head) return null
            let index = 0
            const stack = []
            let cur = head
            while(index < n && cur) {
                stack.push(cur)
                cur = cur.next
                index++
            }
            index--
            let h= stack.pop()
            // console.log(h)
            let pre= h
            while(index>=0) {
                index--
                h.next = stack.pop()
                h = h.next
                console.log(h)
            }
            head.next = reverseAtGj(cur, n)
            return pre
        }
        console.log(reverseAtGj(h1, 3))
        // console.log()
    </script>
</body>
</html>